package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.pojo.dto.ColumnSetAddDTO;
import com.xbongbong.paas.pojo.dto.ColumnSetListDTO;
import com.xbongbong.paas.pojo.dto.DelColumnSetAddDTO;
import com.xbongbong.paas.pojo.dto.DelColumnSetListDTO;
import com.xbongbong.paas.pojo.dto.UpdateMobileModeDTO;
import com.xbongbong.paas.pojo.dto.UserConfigAddDTO;
import com.xbongbong.paas.pojo.vo.ColumnSetAddVO;
import com.xbongbong.paas.pojo.vo.ColumnSetListVO;
import com.xbongbong.paas.pojo.vo.DelColumnSetAddVO;
import com.xbongbong.paas.pojo.vo.DelColumnSetListVO;
import com.xbongbong.paas.pojo.vo.MobileModeGetVO;
import com.xbongbong.paas.pojo.vo.UpdateMobileModeVO;
import com.xbongbong.paas.pojo.vo.UserConfigAddVO;
import com.xbongbong.paas.service.GuideLableService;
import com.xbongbong.paas.service.LaneColumnConfigService;
import com.xbongbong.paas.service.UserConfigService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.config.pojo.dto.GuideLabelIsShowDTO;
import com.xbongbong.pro.config.pojo.dto.GuideLabelSetValueDTO;
import com.xbongbong.pro.config.pojo.vo.GuideLabelIsShowVO;
import com.xbongbong.pro.config.pojo.vo.GuideLabelSetValueVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author: wufeng
 * @date: 2018/11/23 18:31
 * @desrcption: 配置设置
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/config")
public class ConfigController {

    @Resource
    private UserConfigService userConfigService;

    @Resource
    private GuideLableService guideLableService;
    @Resource
    private LaneColumnConfigService laneColumnConfigService;


    /**
     * 列表设置
     * @param columnSetAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/column/set/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String columnSetAdd(@RequestBody @Valid ColumnSetAddDTO columnSetAddDTO, BindingResult br) {
        XbbResponse<ColumnSetAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ColumnSetAddVO columnSetAddVO = userConfigService.columnSetAdd(columnSetAddDTO);
                response = new XbbResponse<>(columnSetAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 获取列表
     * @param columnSetListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/column/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String columnSetList(@RequestBody @Valid ColumnSetListDTO columnSetListDTO, BindingResult br) {
        XbbResponse<ColumnSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ColumnSetListVO columnSetListVO = userConfigService.columnSetList(columnSetListDTO);
                response = new XbbResponse<>(columnSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 泳道视图获取显示列表
     * @param columnSetListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/lane/column/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneColumnList(@RequestBody @Valid ColumnSetListDTO columnSetListDTO, BindingResult br) {
        XbbResponse<ColumnSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ColumnSetListVO columnSetListVO = laneColumnConfigService.laneColumnList(columnSetListDTO);
                response = new XbbResponse<>(columnSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }



    /**
     * 泳道视图列设置
     * @param columnSetAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/lane/column/set", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String laneColumnSet(@RequestBody @Valid ColumnSetAddDTO columnSetAddDTO, BindingResult br) {
        XbbResponse<ColumnSetAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                ColumnSetAddVO columnSetAddVO = laneColumnConfigService.laneColumnSet(columnSetAddDTO);
                response = new XbbResponse<>(columnSetAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回收站列表设置
     * @param delColumnSetAddDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/del/column/set/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delColumnSetAdd(@RequestBody @Valid DelColumnSetAddDTO delColumnSetAddDTO, BindingResult br) {
        XbbResponse<DelColumnSetAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DelColumnSetAddVO delColumnSetAddVO = userConfigService.delColumnSetAdd(delColumnSetAddDTO);
                response = new XbbResponse<>(delColumnSetAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    /**
     * 回收站获取列表设置
     * @param delColumnSetListDTO
     * @param br
     * @return
     */
    @RequestMapping(value = "/del/column/set/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String delColumnSetList(@RequestBody @Valid DelColumnSetListDTO delColumnSetListDTO, BindingResult br) {
        XbbResponse<DelColumnSetListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                DelColumnSetListVO delColumnSetListVO = userConfigService.delColumnSetList(delColumnSetListDTO);
                response = new XbbResponse<>(delColumnSetListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/guide/label/setValue", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String guideLabelSetValue(@RequestBody @Valid GuideLabelSetValueDTO guideLabelSetValueDTO, BindingResult br) {
        XbbResponse<GuideLabelSetValueVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GuideLabelSetValueVO guideLabelSetValueVO = guideLableService.guideLabelSetValue(guideLabelSetValueDTO);
                response = new XbbResponse<>(guideLabelSetValueVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/guide/label/isShow", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String guideLabelIsShow(@RequestBody @Valid GuideLabelIsShowDTO guideLabelIsShowDTO, BindingResult br) {
        XbbResponse<GuideLabelIsShowVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                GuideLabelIsShowVO guideLabelIsShowVO = guideLableService.guideLabelIsShow(guideLabelIsShowDTO);
                response = new XbbResponse<>(guideLabelIsShowVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/update/mobileMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String updateMobileMode(@RequestBody @Valid UpdateMobileModeDTO updateMobileModeDTO, BindingResult br) {
        XbbResponse<UpdateMobileModeVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UpdateMobileModeVO updateMobileModeVO = userConfigService.updateMobileMode(updateMobileModeDTO);
                response = new XbbResponse<>(updateMobileModeVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/get/mobileMode", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getMobileMode(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<MobileModeGetVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                MobileModeGetVO mobileModeGetVO = userConfigService.getMobileMode(baseDTO);
                response = new XbbResponse<>(mobileModeGetVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }

    @RequestMapping(value = "/product/openOnlyShowSpecifitions", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String openOnlyShowSpecifitions(@RequestBody @Valid UserConfigAddDTO userConfigAddDTO, BindingResult br) {
        XbbResponse<UserConfigAddVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                UserConfigAddVO userConfigAddVO = userConfigService.openOnlyShowSpecifitions(userConfigAddDTO);
                response = new XbbResponse<>(userConfigAddVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
